package com.altekis.kanbankaze.client;

import com.altekis.kanbankaze.shared.UserStory;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.event.dom.client.HasAllMouseHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasTouchEndHandlers;
import com.google.gwt.event.dom.client.HasTouchStartHandlers;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.dom.client.TouchEndEvent;
import com.google.gwt.event.dom.client.TouchEndHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * Card view of a User Story
 * 
 */
public class UserStoryCardView extends Composite implements
		HasTouchStartHandlers, HasTouchEndHandlers, HasAllMouseHandlers,
		UserStoryCardPresenter.Display {

	interface UserStoryCardViewUiBinder extends
			UiBinder<Widget, UserStoryCardView> {
	}
	public static final String CLASSNAME = "lifted";

	private static UserStoryCardViewUiBinder uiBinder = GWT
			.create(UserStoryCardViewUiBinder.class);

	@UiField
	Label descriptionField;

	@UiField
	Button editButton;
	private boolean expanded = false;
	@UiField
	Button flipButton;
	@UiField
	Button flipButton2;
	@UiField
	Label nameField;
	@UiField
	Label titleField;
	protected UserStory userStory;

	public UserStoryCardView(UserStory userStory) {
		this.userStory = userStory;
		initWidget(uiBinder.createAndBindUi(this));
		//getElement().getStyle().setPosition(Position.ABSOLUTE);
		getElement().getStyle().setOverflow(Overflow.VISIBLE);
	}

	@Override
	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
		return addDomHandler(handler, MouseDownEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
		return addDomHandler(handler, MouseMoveEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
		return addDomHandler(handler, MouseUpEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
		return addDomHandler(handler, MouseWheelEvent.getType());
	}

	@Override
	public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
		return addDomHandler(handler, TouchEndEvent.getType());
	}

	@Override
	public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
		return addDomHandler(handler, TouchStartEvent.getType());
	}

	@Override
	public void expandCollapse() {
		if (expanded) {
			this. addStyleName(CLASSNAME);
			this.removeStyleName("expanded");
		} else {
			this.removeStyleName(CLASSNAME);
			this.addStyleName("expanded");
		}
		expanded = !expanded;
	}

	@Override
	public HasText getDescriptionField() {
		return descriptionField;
	}

	@Override
	public Button getEditButton() {
		return editButton;
	}

	@Override
	public HasClickHandlers getFlipButton() {
		return flipButton;
	}
	
	@Override
	public HasClickHandlers getFlipButton2() {
		return flipButton2;
	}

	@Override
	public HasText getNameField() {
		return nameField;
	}

	@Override
	public HasText getTitleField() {
		return titleField;
	}

	public UserStory getUserStory() {
		return userStory;
	}

	public void setPixelPosition(int x, int y) {
		final Style style = this.getElement().getStyle();
		style.setProperty("left", x / 10 + "em");
		style.setProperty("top", y / 10 + "em");
	}

	public void setUserStory(UserStory userStory) {
		this.userStory = userStory;
	}

	@Override
	public void flip() {
		if (this.getStyleName().contains("flipped")) {
			this.removeStyleName("flipped");
		} else {
			this.addStyleName("flipped");
		}
	}
}
